package com.icss.th.paper.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.th.common.util.SecureUtil;
import com.icss.th.db.pojo.PageInfo;
import com.icss.th.fulltext.FullTextSearchFactory;
import com.icss.th.fulltext.IFullTextSearch;
import com.icss.th.fulltext.pojo.SearchResult;

/**
 * Servlet implementation class PaperServlet
 */
@WebServlet("/paper")
public class PaperServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String id = request.getParameter("id");
		String pnum = request.getParameter("pnum");
		
		PageInfo pi = new PageInfo();
		if (pnum != null && !"".equals(pnum))
		{
			pi.setPageNum(Integer.valueOf(pnum));
		}
		
		List<SearchResult> list = null;
		long beginTime = new Date().getTime();
		IFullTextSearch search = FullTextSearchFactory.getInstance();
		if (search != null)
		{
			list = search.getExamByPaperId(SecureUtil.decryptPassword(id), pi);
		}
		float spendTime = (new Date().getTime() - beginTime)/1000f;
		
		String name = "";
		if (list != null && !list.isEmpty())
		{
			name = list.get(0).getSourceName();
		}
		
		request.setAttribute("id", id);
		request.setAttribute("name", name);
		request.setAttribute("list", list);
		request.setAttribute("pageInfo", pi);
		request.setAttribute("spendTime", String.valueOf(spendTime));
		
		request.getRequestDispatcher("/paper.jsp").forward(request, response);
	}
}
